/* -----------------------------------------------------------------------------

PicoModel Library

Copyright (c) 2002, Randy Reddig & seaw0lf
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the names of the copyright holders nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------- */

// This header file is based from the following:

/*
	FlexModel.H - Header file for FlexModel file structure

	By Chris Burke
	serotonin@earthlink.net
*/

#ifndef	__PM_FM_H__
#define	__PM_FM_H__

#include "picointernal.h"


//
//	Absolute limits (from QData / QMView source)
//
#define	MAX_FM_TRIANGLES	2048
#define MAX_FM_VERTS		2048
#define MAX_FM_FRAMES		2048
#define MAX_FM_SKINS		64
#define	MAX_FM_SKINNAME		64
#define MAX_FM_MESH_NODES	16

#define DTRIVERTX_V0   0
#define DTRIVERTX_V1   1
#define DTRIVERTX_V2   2
#define DTRIVERTX_LNI  3
#define DTRIVERTX_SIZE 4

#define SKINPAGE_WIDTH 640
#define SKINPAGE_HEIGHT 480

#define ENCODED_WIDTH_X 92
#define ENCODED_WIDTH_Y 475
#define ENCODED_HEIGHT_X 128
#define ENCODED_HEIGHT_Y 475

#define SCALE_ADJUST_FACTOR 0.96

#define INFO_HEIGHT 5
#define INFO_Y (SKINPAGE_HEIGHT-INFO_HEIGHT)

#ifndef byte
	#define byte unsigned char
#endif


//
//	Generic header on every chunk
//
#define	FM_MAXCHUNKIDENT	32L
typedef struct
{
	char			ident[FM_MAXCHUNKIDENT];
	unsigned int		version;
	unsigned int		size;
} fm_chunk_header_t;

//
//	The format of the "header" chunk
//
#define	FM_HEADERCHUNKNAME	"header"
#define	FM_HEADERCHUNKVER	2
#define	FM_HEADERCHUNKSIZE	40
typedef struct
{
	int			skinWidth;		// in pixels
	int			skinHeight;		// in pixels
	int			frameSize;		// size of each frame (in bytes)
	int			numSkins;		// number of skins
	int			numXYZ;			// number of unique vertices in 3D space
	int			numST;			// number of unique vertices in texture space
	int			numTris;		// number of unique triangles
	int			numGLCmds;		// # 32-bit elements in strip/fan command list
	int			numFrames;		// number of animation frames
	int			numMeshNodes;		// number of mesh nodes
} fm_header_t;

//
//	The format of an entry in the "skin" chunk.
//	The number of entries is given in the fmheader chunk
//
#define	FM_SKINCHUNKNAME	"skin"
#define	FM_SKINCHUNKVER		1
#define	FM_MAXPATHLENGTH	64L
#define	FM_SKINPATHSIZE	(FM_MAXPATHLENGTH)
typedef struct
{
	char				path[FM_SKINPATHSIZE];	//	path, relative to 'base'
} fm_skinpath_t;

//
//	The format of the "st coord" chunk. This is a list
//	of unique skin texture (u, v) coordinates to be mapped
//	to verteces of the model
//
#define FM_STCOORDCHUNKNAME	"st coord"
#define FM_STCOORDCHUNKVER	1
#define	FM_STCOORDUVSIZE	(2L + 2L)

typedef struct
{
	short	s;
	short	t;
} fm_st_t;

//
//	The format of the "tris" chunk. This is a list of vertex indeces
//	in 3D space, and the corresponding vertex indeces in texture space.
//
#define FM_TRISCHUNKNAME	"tris"
#define FM_TRISCHUNKVER		1
#define	FM_TRISINFOSIZE		(2L*3 + 2L*3)

typedef struct
{
	short	index_xyz[3];
	short	index_st[3];
} fm_xyz_st_t;


//
//	The format of the "frames" chunk. This is a list of animation
//	frames, each specifying the coordinates and "light normal" index
//	of every vertex of the model in 3D space.
//
#define FM_FRAMESCHUNKNAME	"frames"
#define FM_FRAMESCHUNKVER	1

#define FM_NUMVERTEXNORMALS	162

// Frame info
typedef struct
{
	byte			v[3];				//	scaled by header info
	byte			lightnormalindex;	//	index in canned table of closest vertex normal
} fm_vert_normal_t;

typedef struct
{
	float			scale[3];			//	multiply byte verts by this
	float			translate[3];		//	then add this
	char			name[16];			//	frame name
} fm_framehdr_t;

typedef struct
{
	fm_framehdr_t		header;				//	One header per frame
	fm_vert_normal_t	verts[1];			//	variable number of these
} fm_frame_t;

typedef struct
{
	fm_chunk_header_t	*fm_header_hdr;
	fm_header_t		*fm_header;
	fm_chunk_header_t	*fm_skin_hdr;
	fm_skinpath_t		*fm_skin;
	fm_chunk_header_t	*fm_st_hdr;
	fm_st_t			*fm_st;
	fm_chunk_header_t	*fm_tri_hdr;
	fm_xyz_st_t		*fm_tri;
	fm_chunk_header_t	*fm_frame_hdr;
	fm_frame_t		*fm_frame;
} fm_t;

float	fm_normals[FM_NUMVERTEXNORMALS][3] = {
  {-0.525731f, 0.000000f, 0.850651f},
  {-0.442863f, 0.238856f, 0.864188f},
  {-0.295242f, 0.000000f, 0.955423f},
  {-0.309017f, 0.500000f, 0.809017f},
  {-0.162460f, 0.262866f, 0.951056f},
  {0.000000f, 0.000000f, 1.000000f},
  {0.000000f, 0.850651f, 0.525731f},
  {-0.147621f, 0.716567f, 0.681718f},
  {0.147621f, 0.716567f, 0.681718f},
  {0.000000f, 0.525731f, 0.850651f},
  {0.309017f, 0.500000f, 0.809017f},
  {0.525731f, 0.000000f, 0.850651f},
  {0.295242f, 0.000000f, 0.955423f},
  {0.442863f, 0.238856f, 0.864188f},
  {0.162460f, 0.262866f, 0.951056f},
  {-0.681718f, 0.147621f, 0.716567f},
  {-0.809017f, 0.309017f, 0.500000f},
  {-0.587785f, 0.425325f, 0.688191f},
  {-0.850651f, 0.525731f, 0.000000f},
  {-0.864188f, 0.442863f, 0.238856f},
  {-0.716567f, 0.681718f, 0.147621f},
  {-0.688191f, 0.587785f, 0.425325f},
  {-0.500000f, 0.809017f, 0.309017f},
  {-0.238856f, 0.864188f, 0.442863f},
  {-0.425325f, 0.688191f, 0.587785f},
  {-0.716567f, 0.681718f, -0.147621f},
  {-0.500000f, 0.809017f, -0.309017f},
  {-0.525731f, 0.850651f, 0.000000f},
  {0.000000f, 0.850651f, -0.525731f},
  {-0.238856f, 0.864188f, -0.442863f},
  {0.000000f, 0.955423f, -0.295242f},
  {-0.262866f, 0.951056f, -0.162460f},
  {0.000000f, 1.000000f, 0.000000f}, 
  {0.000000f, 0.955423f, 0.295242f},
  {-0.262866f, 0.951056f, 0.162460f},
  {0.238856f, 0.864188f, 0.442863f},
  {0.262866f, 0.951056f, 0.162460f}, 
  {0.500000f, 0.809017f, 0.309017f},
  {0.238856f, 0.864188f, -0.442863f},
  {0.262866f, 0.951056f, -0.162460f},
  {0.500000f, 0.809017f, -0.309017f},
  {0.850651f, 0.525731f, 0.000000f},
  {0.716567f, 0.681718f, 0.147621f},
  {0.716567f, 0.681718f, -0.147621f},
  {0.525731f, 0.850651f, 0.000000f}, 
  {0.425325f, 0.688191f, 0.587785f},
  {0.864188f, 0.442863f, 0.238856f}, 
  {0.688191f, 0.587785f, 0.425325f},
  {0.809017f, 0.309017f, 0.500000f}, 
  {0.681718f, 0.147621f, 0.716567f},
  {0.587785f, 0.425325f, 0.688191f}, 
  {0.955423f, 0.295242f, 0.000000f},
  {1.000000f, 0.000000f, 0.000000f}, 
  {0.951056f, 0.162460f, 0.262866f},
  {0.850651f, -0.525731f, 0.000000f}, 
  {0.955423f, -0.295242f, 0.000000f},
  {0.864188f, -0.442863f, 0.238856f}, 
  {0.951056f, -0.162460f, 0.262866f},
  {0.809017f, -0.309017f, 0.500000f}, 
  {0.681718f, -0.147621f, 0.716567f},
  {0.850651f, 0.000000f, 0.525731f},
  {0.864188f, 0.442863f, -0.238856f}, 
  {0.809017f, 0.309017f, -0.500000f},
  {0.951056f, 0.162460f, -0.262866f}, 
  {0.525731f, 0.000000f, -0.850651f},
  {0.681718f, 0.147621f, -0.716567f},
  {0.681718f, -0.147621f, -0.716567f}, 
  {0.850651f, 0.000000f, -0.525731f},
  {0.809017f, -0.309017f, -0.500000f}, 
  {0.864188f, -0.442863f, -0.238856f},
  {0.951056f, -0.162460f, -0.262866f}, 
  {0.147621f, 0.716567f, -0.681718f},
  {0.309017f, 0.500000f, -0.809017f}, 
  {0.425325f, 0.688191f, -0.587785f},
  {0.442863f, 0.238856f, -0.864188f}, 
  {0.587785f, 0.425325f, -0.688191f},
  {0.688191f, 0.587785f, -0.425325f}, 
  {-0.147621f, 0.716567f, -0.681718f},
  {-0.309017f, 0.500000f, -0.809017f}, 
  {0.000000f, 0.525731f, -0.850651f},
  {-0.525731f, 0.000000f, -0.850651f}, 
  {-0.442863f, 0.238856f, -0.864188f},
  {-0.295242f, 0.000000f, -0.955423f}, 
  {-0.162460f, 0.262866f, -0.951056f},
  {0.000000f, 0.000000f, -1.000000f}, 
  {0.295242f, 0.000000f, -0.955423f},
  {0.162460f, 0.262866f, -0.951056f}, 
  {-0.442863f, -0.238856f, -0.864188f},
  {-0.309017f, -0.500000f, -0.809017f}, 
  {-0.162460f, -0.262866f, -0.951056f},
  {0.000000f, -0.850651f, -0.525731f}, 
  {-0.147621f, -0.716567f, -0.681718f},
  {0.147621f, -0.716567f, -0.681718f}, 
  {0.000000f, -0.525731f, -0.850651f},
  {0.309017f, -0.500000f, -0.809017f}, 
  {0.442863f, -0.238856f, -0.864188f},
  {0.162460f, -0.262866f, -0.951056f}, 
  {0.238856f, -0.864188f, -0.442863f},
  {0.500000f, -0.809017f, -0.309017f}, 
  {0.425325f, -0.688191f, -0.587785f},
  {0.716567f, -0.681718f, -0.147621f}, 
  {0.688191f, -0.587785f, -0.425325f},
  {0.587785f, -0.425325f, -0.688191f}, 
  {0.000000f, -0.955423f, -0.295242f},
  {0.000000f, -1.000000f, 0.000000f}, 
  {0.262866f, -0.951056f, -0.162460f},
  {0.000000f, -0.850651f, 0.525731f}, 
  {0.000000f, -0.955423f, 0.295242f},
  {0.238856f, -0.864188f, 0.442863f}, 
  {0.262866f, -0.951056f, 0.162460f},
  {0.500000f, -0.809017f, 0.309017f}, 
  {0.716567f, -0.681718f, 0.147621f},
  {0.525731f, -0.850651f, 0.000000f}, 
  {-0.238856f, -0.864188f, -0.442863f},
  {-0.500000f, -0.809017f, -0.309017f}, 
  {-0.262866f, -0.951056f, -0.162460f},
  {-0.850651f, -0.525731f, 0.000000f}, 
  {-0.716567f, -0.681718f, -0.147621f},
  {-0.716567f, -0.681718f, 0.147621f}, 
  {-0.525731f, -0.850651f, 0.000000f},
  {-0.500000f, -0.809017f, 0.309017f}, 
  {-0.238856f, -0.864188f, 0.442863f},
  {-0.262866f, -0.951056f, 0.162460f}, 
  {-0.864188f, -0.442863f, 0.238856f},
  {-0.809017f, -0.309017f, 0.500000f}, 
  {-0.688191f, -0.587785f, 0.425325f},
  {-0.681718f, -0.147621f, 0.716567f}, 
  {-0.442863f, -0.238856f, 0.864188f},
  {-0.587785f, -0.425325f, 0.688191f}, 
  {-0.309017f, -0.500000f, 0.809017f},
  {-0.147621f, -0.716567f, 0.681718f}, 
  {-0.425325f, -0.688191f, 0.587785f},
  {-0.162460f, -0.262866f, 0.951056f}, 
  {0.442863f, -0.238856f, 0.864188f},
  {0.162460f, -0.262866f, 0.951056f}, 
  {0.309017f, -0.500000f, 0.809017f},
  {0.147621f, -0.716567f, 0.681718f}, 
  {0.000000f, -0.525731f, 0.850651f},
  {0.425325f, -0.688191f, 0.587785f}, 
  {0.587785f, -0.425325f, 0.688191f},
  {0.688191f, -0.587785f, 0.425325f}, 
  {-0.955423f, 0.295242f, 0.000000f},
  {-0.951056f, 0.162460f, 0.262866f}, 
  {-1.000000f, 0.000000f, 0.000000f},
  {-0.850651f, 0.000000f, 0.525731f}, 
  {-0.955423f, -0.295242f, 0.000000f},
  {-0.951056f, -0.162460f, 0.262866f}, 
  {-0.864188f, 0.442863f, -0.238856f},
  {-0.951056f, 0.162460f, -0.262866f}, 
  {-0.809017f, 0.309017f, -0.500000f},
  {-0.864188f, -0.442863f, -0.238856f},
  {-0.951056f, -0.162460f, -0.262866f},
  {-0.809017f, -0.309017f, -0.500000f}, 
  {-0.681718f, 0.147621f, -0.716567f},
  {-0.681718f, -0.147621f, -0.716567f}, 
  {-0.850651f, 0.000000f, -0.525731f},
  {-0.688191f, 0.587785f, -0.425325f}, 
  {-0.587785f, 0.425325f, -0.688191f},
  {-0.425325f, 0.688191f, -0.587785f}, 
  {-0.425325f, -0.688191f, -0.587785f},
  {-0.587785f, -0.425325f, -0.688191f}, 
  {-0.688191f, -0.587785f, -0.425325f},
};

#endif
